package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Dimensions;
import com.tanhua.dubbo.server.pojo.Report;
import com.tanhua.dubbo.server.pojo.SimilarYou;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created by ${smh} on 2020/12/13.
 */

@Service(version = "1.0.0")
public class SoluApiImpl implements SoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 根据userid查询报告
     * @param userId
     * @return
     */
    @Override
    public Report queryReportById(Long userId) {

        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created")));
        Report report = this.mongoTemplate.findOne(query, Report.class);
        return report;
    }

    /**
     * 保存报告
     * @param score
     * @param userId
     * @return
     */
    @Override
    public String saveReport(Integer score, Long userId) {

        Report report = new Report();
        report.setUserId(userId);
        report.setId(new ObjectId());

        List<Dimensions> dimensionsList = new ArrayList<>();
        Dimensions dimensions1 = new Dimensions("外向","80%");
        Dimensions dimensions2 = new Dimensions("判断","70%");
        Dimensions dimensions3 = new Dimensions("抽象","90%");
        Dimensions dimensions4 = new Dimensions("理性","60%");

        dimensionsList.add(dimensions1);
        dimensionsList.add(dimensions2);
        dimensionsList.add(dimensions3);
        dimensionsList.add(dimensions4);
        report.setDimensions(dimensionsList);


        SimilarYou similarYou1 = new SimilarYou(3,"http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/9.jpg");
        SimilarYou similarYou2 = new SimilarYou(6,"http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/11.jpg");
        SimilarYou similarYou3 = new SimilarYou(9,"http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/8.jpg");
        SimilarYou similarYou4 = new SimilarYou(12,"http://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/logo/18.jpg");
        List<SimilarYou> similarYouList = new ArrayList<>();
        similarYouList.add(similarYou1);
        similarYouList.add(similarYou2);
        similarYouList.add(similarYou3);
        similarYouList.add(similarYou4);
        report.setSimilarYous(similarYouList);


        //根据得到的总分判断类型
        if(score < 21){
            report.setConclusion("猫头鹰：他们的共同特质为重计划、条理、细节精准。在行为上，表现出喜欢理性思考与分析、较重视制度、结构、规范。他们注重执行游戏规则、循规蹈矩、巨细靡遗、重视品质、敬业负责");
            report.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/owl.png");

        }else if(score >= 21 && score <= 40){
            report.setConclusion("白兔型：平易近人、敦厚可靠、避免冲突与不具批判性。在行为上，表现出不慌不忙、冷静自持的态度。他们注重稳定与中长程规划，现实生活中，常会反思自省并以和谐为中心，即使面对困境，亦能泰然自若，从容应付");
            report.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/rabbit.png");

        }else if(score >= 41 && score <= 55){
            report.setConclusion("狐狸型 ：人际关系能力极强，擅长以口语表达感受而引起共鸣，很会激励并带动气氛。他们喜欢跟别人互动，重视群体的归属感，基本上是比较「人际导向」。由于他们富同理心并乐于分享，具有很好的亲和力，在服务业、销售业、传播业及公共关系等领域中，狐狸型的领导者都有很杰出的表现");
            report.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/fox.png");

        }else if(score >= 56){
            report.setConclusion("狮子型：性格为充满自信、竞争心强、主动且企图心强烈，是个有决断力的领导者。一般而言，狮子型的人胸怀大志，勇于冒险，看问题能够直指核心，并对目标全力以赴。\n" +
                    "他们在领导风格及决策上，强调权威与果断，擅长危机处理，此种性格最适合开创性与改革性的工作\n");
            report.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/lion.png");

        }
        report.setCreated(System.currentTimeMillis());
        this.mongoTemplate.save(report);
        //返回报告的id
        return report.getId().toHexString();
    }

    @Override
    public Report selectReport(String id) {
        Report report = this.mongoTemplate.findById(new ObjectId(id), Report.class);
        return report;
    }
}
